<?php

if($dbType = 'mysql'){
	include_once 'CF_mysql.php';
}else if($dbType = 'oracle'){

}

	
function lookup_value ($res_column, $tablestring, $whereclause){
	global $dbh;
	$query_lookup = "SELECT $res_column FROM $tablestring WHERE $whereclause LIMIT 1";
	/*** fetch into an PDOStatement object ***/
	try{
		$stmt = $dbh->query($query_lookup);
		/*** echo number of columns ***/
		if (empty($stmt)) {
		return 'error';
		}
		$result = $stmt->fetch(PDO::FETCH_NUM);
	return $result[0];
	}
	catch(PDOException $e)
	{	
	echo $e->getMessage();
	}	
	
}
	
	
function DEBUGlookup_value ($res_column, $tablestring, $whereclause){
	global $dbh;
	$query_lookup = "SELECT $res_column FROM $tablestring WHERE $whereclause LIMIT 1";
	/*** fetch into an PDOStatement object ***/
	print $query_lookup;
	exit;
	try{
		$stmt = $dbh->query($query_lookup);
		/*** echo number of columns ***/
		if (empty($stmt)) {
		return 'error';
		}
		$result = $stmt->fetch(PDO::FETCH_NUM);
	return $result[0];
	}
	catch(PDOException $e)
	{	
	echo $e->getMessage();
	}	
	
}	


//This function can be used to return the maximum value (usefull for inserts when we cant do it all in one insert)
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function get_max_value ($res_column, $tablestring, $whereclause) {
	$query_lookup = "SELECT MAX($res_column) FROM $tablestring WHERE $whereclause LIMIT 1";
	global $dbh;
	/*** fetch into an PDOStatement object ***/
	try{
			$stmt = $dbh->query($query_lookup);
			/*** echo number of columns ***/
			if (empty($stmt)) {
				return 'error';
			}
			$result = $stmt->fetch(PDO::FETCH_NUM);
			return $result[0];			
		}

	catch(PDOException $e)
	{	
	echo $e->getMessage();
	}	
	
}	
	
	
//This function can be used to return the maximum value (usefull for inserts when we cant do it all in one insert)
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function get_query_count ($thequery) {
	global $dbh;
	$thequery = str_replace("\n", " ", $thequery);
		
	$thequery = preg_replace("/SELECT.+FROM/", "SELECT count(*) FROM", $thequery);	
	/*** fetch into an PDOStatement object ***/
	try{
			$stmt = $dbh->query($thequery);
			/*** echo number of columns ***/
			if (empty($stmt)) {
				return 'error';
			}
			$result = $stmt->fetch(PDO::FETCH_NUM);
			return $result[0];			
		}

	catch(PDOException $e)
	{	
	echo $e->getMessage();
	}	
	
}		
	
// This function checks if a value exist or not in the DB, returns 1 on data and 0 of empty set.
function check_for_data($query_lookup){
	global $dbh;
	
	try{
			$stmt = $dbh->query($query_lookup);
			/*** echo number of rows ***/
			if (empty($stmt)) {
				return 0;
			}
			$result = $stmt->fetch(PDO::FETCH_NUM);
			if(is_array($result)){
				return 1;
				}
			else{
				return 0;
				}
		}
	catch(PDOException $e)
	{	
	echo $e->getMessage();
	}	

}	
	
	
	
	
//This function can be used to return a delimited list of values from a query.
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function get_value_list($query_lookup, $delimiter) {
	global $dbh;
	try{
	    /*** fetch into an PDOStatement object ***/
		$stmt = $dbh->query($query_lookup);

		/*** echo number of columns ***/
		$result = $stmt->fetchall(PDO::FETCH_NUM);
				
	}
	catch(PDOException $e)
		{	
		echo $e->getMessage();
		}		
	if(is_array($result)){
		$index = 0;
		foreach($result AS $row){
			if($index > 0) {
				$value .= $delimiter;
			}
			$value .=  $row[0] ;
			$index ++;
		}
		return $value;
	}
	else{
	return;
	}
	
	
	}
	
	

//GENERIC WRAPPER FUNCTION for Query Exec
function do_query($query) {
	global $dbh;
	try{
	    /*** fetch into an PDOStatement object ***/
		$stmt = $dbh->prepare($query);

		/*** echo number of columns ***/
		$stmt->execute();
		$result =  $stmt->rowCount();
		return $result;
				
	}
	catch(PDOException $e)
		{	
		echo $e->getMessage();
		}
}

function get_array_query($thequery){
	global $dbh;
	try{
	    /*** fetch into an PDOStatement object ***/
		$stmt = $dbh->query($thequery);	

		/*** Get data ***/
		if(!empty($stmt)){
			$result = $stmt->fetchall(PDO::FETCH_ASSOC);
		} else{
			$result = NULL;
		}
	}
	catch(PDOException $e)
		{	
		echo $e->getMessage();
		}	
	return $result;
}

	
	
?>